#ifndef _DETU_MEDIA_PCIV_CHIP_IMP_H_
#define _DETU_MEDIA_PCIV_CHIP_IMP_H_

#include "pciv_chip.h"
#include "pciv_port.h"
#include "pciv_chn_imp.h"
#include "mutex.h"

#include "hi_common.h"
#include "hi_comm_pciv.h"

namespace detu_media
{

class PcivMgrImp;

class PcivChipImp : public PcivChip
{
protected:
    PcivChipImp(const int localChipId, const int remoteChipId);

    virtual ~PcivChipImp();

	friend class PcivMgrImp;

public:
    virtual S_Result getId(int &id);

	virtual S_Result getChn(const std::string& name, PcivChn** ppPcivChn);

	virtual S_Result setSystemTime(const std :: string & timeString, Json :: Value & response);

	virtual S_Result setConfig(const std :: string & name, const Json :: Value & config, Json :: Value & response, const Int32_t delaySaveInMicroSecs, CallbackFunc cb = NULL, void *cbParam = NULL);

	virtual S_Result getConfig(const std :: string & name, Json :: Value & config, Json :: Value & response);

	virtual S_Result setTempConfig(const std :: string & name, const Json :: Value & config, Json :: Value & response, CallbackFunc cb = NULL, void *cbParam = NULL);

	virtual S_Result getTempConfig(const std :: string & name, Json :: Value & config, Json :: Value & response);

	virtual S_Result resetDefault(const std :: string & name, Json :: Value & response, CallbackFunc cb = NULL, void *cbParam = NULL);

    virtual S_Result getRemoteChnId(const std :: string & name, int &id);

	virtual S_Result startRemoteChn(const std :: string & name, const void* attr, const Uint32_t attrSize);

	virtual S_Result stopRemoteChn(const std :: string & name);

	virtual S_Result open(Uint64_t& file, const std::string& fileName, const std::string& mode);
	
	virtual S_Result close(Uint64_t& file);

	virtual S_Result getFileSize(const Uint64_t file, Uint32_t& size);
	
	virtual S_Result read(const Uint64_t file, void* pAddr, Uint32_t& size);
	
	virtual S_Result write(const Uint64_t file, const void* pAddr, Uint32_t& size);

	virtual S_Result flush(const Uint64_t file);

private:
	static void* onRecvCmd(void* param);

	void onRecvCmd1(void);

	S_Result onDispatch(void);

	S_Result onSetSystemTime(const std :: string & timeString, Json::Value& response);

	S_Result onSetConfig(const std :: string & name, const Json :: Value & config, Json :: Value & response, const Int32_t delaySaveInMicroSecs = 0);

	S_Result onGetConfig(const std :: string & name, Json :: Value & config, Json :: Value & response);

	S_Result onSetTempConfig(const std :: string & name, const Json :: Value & config, Json :: Value & response);

	S_Result onGetTempConfig(const std :: string & name, Json :: Value & config, Json :: Value & response);

	S_Result onResetDefault(const std :: string & name, Json :: Value & response);

    S_Result onGetRemoteChnId(const std :: string & name, int &id);

	S_Result onStartRemoteChn(const std :: string & name, const void* attr, const Uint32_t attrSize);

	S_Result onStopRemoteChn(const std :: string & name);

	S_Result onOpen(Uint64_t& file, const std::string& fileName, const std::string& mode);
	
	S_Result onClose(const   Uint64_t file);

	S_Result onGetFileSize(const Uint64_t file, Uint32_t& size);
	
	S_Result onRead(const Uint64_t file, void* pAddr, Uint32_t& size);
	
	S_Result onWrite(const Uint64_t file, const void* pAddr, Uint32_t& size);

	S_Result onFlush(const Uint64_t file);

	S_Result blockCall(const Json::Value& request, Json::Value& response, 
		const CallbackFunc cb = NULL, void *param = NULL, 
		const void* requestExt = NULL, const Uint32_t requestExtSize = 0, 
		void** responseExt = NULL, Uint32_t* responseExtSize = NULL);

    S_Result initComm(void);

    S_Result deInitComm(void);

    bool isCommInited(void);

    S_Result init(const Json::Value& config);

    S_Result deInit(void);

    S_Result initChns(const Json::Value& config);

    S_Result deInitChns(void);

    bool isInited(void);

	S_Result waitConnect(void);

	S_Result createListener(void);

	S_Result destroyListener(void);

private:
	typedef std::map<const std::string, PcivChnImp*> ChnMap;

	int 				fLocalChipId;
	int 				fRemoteChipId;

	PcivPort			fCmdPort;
	pthread_cond_t		fCond;
	pthread_t			fListenerId;
	bool				fOnListenning;
	detu_infra::Mutex   fBlockCallMutex;

	ChnMap				fChnMap;

	bool                fCommInited;
    bool                fInited;
    detu_infra::Mutex   fMutex;
};

}

#endif
